package main

import (
	"fmt"
	"sort"
)

type shape struct {
	name       string
	area       float64
	area_data1 float64
	area_data2 float64
}

func New_shape(name string, area_data1 float64, area_data2 float64) *shape {
	var s = shape{}
	allow_name := []string{"triangle", "roundness", "rectangle"}
	for _, v := range allow_name {
		if name == v {
			s.name = v
		}
	}
	switch s.name {
	case "roundness":
		s.area_data1 = area_data1
		s.area_data2 = 3.1415926
	default:
		s.area_data1 = area_data1
		s.area_data2 = area_data2
	}
	return &s
}

func (s *shape) Calculated_area() {
	if s.name == "triangle" {
		s.area = s.area_data1 * s.area_data2 / 2
	} else if s.name == "roundness" {
		s.area = s.area_data1 * s.area_data1 * s.area_data2
	} else if s.name == "rectangle" {
		s.area = s.area_data1 * s.area_data2
	}
}

type Sortshape []*shape

func (a Sortshape) Len() int           { return len(a) }
func (a Sortshape) Swap(i, j int)      { a[i].area, a[j].area = a[j].area, a[i].area }
func (a Sortshape) Less(i, j int) bool { return a[i].area > a[j].area }

func homework3() {
	t1 := New_shape("triangle", 1, 2)
	t2 := New_shape("roundness", 1, 2)
	t3 := New_shape("rectangle", 1, 2)
	t1.Calculated_area()
	t2.Calculated_area()
	t3.Calculated_area()

	a := []*shape{t1, t2, t3}
	for _, v := range a {
		fmt.Println(v.name, v.area)
	}
	fmt.Println("!~!~!~!~!~!~!~!")
	sort.Sort(Sortshape(a))

	for _, v := range a {
		fmt.Println(v.name, v.area)
	}
}
